﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebAPItest.Models.Database;
using WebAPItest.Service;

namespace WebAPItest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {

        //注入jwt服务
        private readonly IJWTService _jwtService;
        private readonly IUserService _userService;
        private readonly WebEnterpriseIIContext _db;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="jwtService"></param>
        public AuthenticationController(IJWTService jwtService, WebEnterpriseIIContext db,IUserService userService)
        {
            _jwtService = jwtService;
            _db = db;
            _userService = userService;
        }
        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name)
        {
            //如果是登录
            //判断用户名和密码是否正确


            //如果正确才创建token  用这个用户名的用户信息去创建token
            return _jwtService.CreateToken(name,0,"","");
        }


        [Authorize]//授权标识
        [HttpGet]
        public void Test()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public object UserInfo()
        {
            
            //获取用户信息
            //数据来源与Token用户信息保存
           /* return new
            {
                Email = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value,
                UserName = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value,
                NickName = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == "NickName").Value,
                UserId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value
             };*/


            //通过userid查询数据库数据
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var user = _db.Users.FirstOrDefault(x => x.UserId == Convert.ToInt32(userId));
            return user;


            //区别
            //加入生成Token
        }

        //注册 把用户信息插入用户表
        [HttpPost]
        public string Reg(User request)
        {
            //保证用户唯一
            //判断数据(用户名为request.UserName)是否存在

            //查询用户表是否有存在的用户名
            if (_userService.ExistUserByUserName(request.UserName))
                return "用户存在";
          var userId= _userService.AddNewUser(request.UserName,request.Email,request.NickName,request.Password);
         if(userId>0)
            {
                return "注册成功";
            }
            return "注册失败";
        }
        [HttpPost]
        public string Login(string username, string password)
        {
            //第一步 从数据库查询用户为username的用户
        //  var user=  _db.Users.FirstOrDefault(x => x.UserName == username);
        var user =_userService.GetUserByUserName(username);
            //如果不存在用户就不在
            if (user == null)
                return "用户不存在";
            //如果存在 判断查询出来的用户密码是否跟 password相等
            if(user.Password != password)
            {
                   //如果不相等  返回密码不正确
                return "密码不正确";
            }
         
            //如果相等就

            //创建Token 返回token给前端
            return _jwtService.CreateToken(user.UserName,user.UserId,user.Email,user.NickName);
        }
    }
}
